<template>
  <div class="mod-role">
    <el-form
      class="formEl"
      :inline="true"
      :model="dataForm"
      @keyup.enter="getDataList()"
      label-width="100px"
    >
      <el-form-item label="比赛状态">
        <el-select
          v-model="dataForm.matchStatusList"
          multiple
          value-key="name"
          filterable
          placeholder="比赛状态"
          clearable
          style="width: 100%"
        >
          <el-option
            v-for="item in matchStatusData"
            :key="item.code"
            :label="item.name"
            :value="item.code"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="比赛类型">
        <el-select
          v-model="dataForm.matchTag"
          value-key="name"
          filterable
          placeholder="比赛类型"
          clearable
        >
          <el-option
            v-for="item in matchTagData"
            :key="item.code"
            :label="item.name"
            :value="item.code"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否历史赛事">
        <el-select
          v-model="dataForm.historyFlag"
          value-key="name"
          filterable
          placeholder="是否历史赛事"
          clearable
        >
          <el-option
            v-for="item in historyFlagData"
            :key="item.code"
            :label="item.name"
            :value="item.code"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button type="success" @click="batchEnableHandle()">发布</el-button>
        <!-- <el-button type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0"
                      >批量删除</el-button> -->
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%"
    >
      <el-table-column type="selection" width="50"> </el-table-column>
      <el-table-column prop="id" label="ID" width="60" align="center">
      </el-table-column>
      <el-table-column
        prop="matchDate"
        width="100"
        label="比赛日期"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="matchTime"
        width="100"
        label="比赛时间"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="matchTag"
        width="140"
        label="比赛类型"
        align="center"
      >
        <template #default="scope">
          <span :style="getColor(scope.row.matchTag)">{{
            scope.row.matchTag
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="leftTeamLogo"
        label="左边球队"
        width="150"
        align="center"
      >
        <template #default="scope">
          <div style="display: inline-flex">
            <div style="display: flex; align-items: center">
              {{ scope.row.leftTeamName }}
            </div>
            <img :src="scope.row.leftTeamLogo" width="63" height="91" />
          </div>
        </template>
      </el-table-column>
      <!--  :show-overflow-tooltip="true" -->
      <el-table-column
        prop="score"
        label="进球数比分"
        width="100"
        align="center"
      >
        <template #default="scope">
          <span
            >{{ scope.row.leftTeamScore }} ：{{
              scope.row.rightTeamScore
            }}</span
          >
        </template>
      </el-table-column>
      <el-table-column
        prop="rightTeamLogo"
        label="右边球队"
        width="150"
        align="center"
      >
        <template #default="scope">
          <div style="display: inline-flex">
            <div style="display: flex; align-items: center">
              {{ scope.row.rightTeamName }}
            </div>
            <img :src="scope.row.rightTeamLogo" width="63" height="91" />
          </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="matchStatus"
        width="100"
        label="比赛状态"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="enableFlag"
        width="140"
        label="是否发布"
        align="center"
      >
        <template #default="scope">
          <el-button
            :type="scope.row.enableFlag === 1 ? 'success' : ''"
            size="small"
            >{{ scope.row.enableFlag === 1 ? "是" : "否" }}</el-button
          >
        </template>
      </el-table-column>
      <el-table-column
        prop="liveFlag"
        width="140"
        label="是否直播"
        align="center"
      >
        <template #default="scope">
          <el-button
            :type="scope.row.liveFlag === 'Y' ? 'success' : ''"
            size="small"
            >{{ scope.row.liveFlag === "Y" ? "是" : "否" }}</el-button
          >
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="center">
        <template v-slot="scope">
          <el-button
            type="text"
            size="small"
            @click="addOrUpdateHandle(scope.row.id)"
            >编辑</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="deleteHandle(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    ></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from "./edit.vue";
import { request } from "@/utils/axios-util";
import { matchStatusData, matchTagData, historyFlagData } from "./config";

export default {
  data() {
    return {
      dataForm: {
        matchStatusList: ["PLAY"],
        matchTag: "GROUP STAGE",
        historyFlag: 0,
      },
      dataList: [],
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      matchStatusData,
      matchTagData,
      historyFlagData,
    };
  },
  components: {
    AddOrUpdate,
  },
  mounted() {
    this.getDataList();
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      const userInfo = JSON.parse(localStorage.getItem("userInfo"));
      request(
        "/admin/leagueMatch/query",
        {
          appCode: userInfo.appCode,
          tenantId: userInfo.tenantId,
          leagueCode: userInfo.leagueCode || "MFEL2024S02",
          matchStatusList: this.dataForm.matchStatusList,
          matchTag: this.dataForm.matchTag,
          historyFlag: this.dataForm.historyFlag,
        },
        "POST"
      ).then(
        (res) => {
          if (res.code === 200) {
            this.dataList = res.data;
          } else {
            this.dataList = [];
          }
          this.dataListLoading = false;
        },
        (resp) => {
          this.$message.error("数据获取失败，", resp);
        }
      );
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    getColor(code) {
      if (!code) return "";
      const obj = matchTagData.find((e) => e.code === code);
      if (!obj) return "";
      return `color: ${obj.color}`;
    },
    // 发布
    batchEnableHandle() {
      if (this.dataListSelections.length === 0) {
        return this.$message.error("请至少选择一条数据");
      }
      const matchIds = this.dataListSelections.map((item) => {
        return item.id;
      });
      request(
        "/admin/leagueMatch/batchEnable",
        {
          matchIds,
          requestId: null,
        },
        "POST"
      ).then(
        (res) => {
          if (res.code === 200) {
            this.getDataList();
          } else {
            this.$message.error(res.msg);
          }
        },
        (resp) => {
          this.$message.error("操作失败，", resp);
        }
      );
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map((item) => {
            return item.id;
          });
      this.$confirm(
        `确定对[id=${ids.join(",")}]进行[${id ? "删除" : "批量删除"}]操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          request("/admin/leagueMatch/delete", { id }, "GET").then(
            (res) => {
              if (res.code === 200) {
                this.$message({
                  message: "操作成功",
                  type: "success",
                  duration: 1500,
                  onClose: () => {
                    this.getDataList();
                  },
                });
              } else {
                this.$message.error(res.msg);
              }
            },
            (resp) => {
              this.$message.error("操作失败，", resp);
            }
          );
        })
        .catch((error) => {
          this.$message.error(error);
        });
    },
  },
};
</script>

<style scoped type="text/sass">
.formEl {
  display: flex;
  flex-wrap: wrap;
}

.page {
  float: right;
  margin-top: 10px;
}
</style>
